"use strict";

/**
 * vtuzx方法类
 */
//------------------------------------------------------------------
import crypto from 'crypto';
import dayjs from "dayjs";
//------------------------------------------------------------------

/**
 * 范围随机数
 *
 * @param from 随机数范围from
 * @param to 随机数范围to
 */
const $randomRange = (from, to) => {
  return Math.floor(Math.random() * (to - from) + from);
};

/**
 * 数值随机数
 *
 * @param length 长度
 */
const $randomNum = length => {
  let val;
  while ((val = Math.random()) < 0.1) {
    // 防止出现0.0X类型的随机数，导致结果位数不正确
  }
  return Math.floor(val * Math.pow(10, length));
};

/**
 * 格式化日期
 *
 * @param date 日期
 * @param format 格式
 */
const $formatDate = (date, format) => {
  if (!date) return "";
  return dayjs(date).format(format);
};

/**
 * 数值格式化(千位逗号分隔)
 *
 * @param value 对象值
 * @param defaultZero 空时默认显示0
 */
const $formatNum = (value, defaultZero) => {
  if (!value) {
    if (defaultZero) {
      value = 0;
    } else {
      return "";
    }
  }
  let nums = value.toString().split("\.");
  let num = nums[0];
  let result = '';
  while (num.length > 3) {
    result = ',' + num.slice(-3) + result;
    num = num.slice(0, num.length - 3);
  }
  if (num) {
    result = num + result;
  }
  return result + (nums.length > 1? ("." + nums[1]): "");
};

/**
 * List转Map处理
 *
 * @param list 转换对象
 * @param keyCol Key列
 * @param valueCol 值列
 */
const $listToMap = (list, keyCol, valueCol) => {
  let ret = {};
  if (!list || !keyCol || !valueCol) return ret;
  for (let i = 0; i < list.length; i++) {
    let key = list[i][keyCol];
    if (!key) continue;
    ret[key] = list[i][valueCol];
  }
  return ret;
};

/**
 * 下载处理
 * @param param 参数
 */
const $download = param => {
  let link = document.createElement("a");
  try {
    link.href = window.URL.createObjectURL(param.data);
    link.download = param.filename;
    document.body.appendChild(link);
    link.click();
  } finally {
    document.body.appendChild(link);
  }
};

/**
 * 浏览器判断是否全屏
 */
const $isFullScreen = () => {
  return document.isFullScreen || document.mozIsFullScreen || document.webkitIsFullScreen;
}

/**
 * 浏览器全屏
 */
const $doFullScreen = () => {
  if (document.documentElement.requestFullScreen) {
    document.documentElement.requestFullScreen();
  } else if (document.documentElement.webkitRequestFullScreen) {
    document.documentElement.webkitRequestFullScreen();
  } else if (document.documentElement.mozRequestFullScreen) {
    document.documentElement.mozRequestFullScreen();
  }
};
/**
 * 浏览器退出全屏
 */
const $exitFullScreen = () => {
  if (document.documentElement.requestFullScreen) {
    document.exitFullScreen();
  } else if (document.documentElement.webkitRequestFullScreen) {
    document.webkitCancelFullScreen();
  } else if (document.documentElement.mozRequestFullScreen) {
    document.mozCancelFullScreen();
  }
};

/**
 * 本地存储
 *
 * @param key 存储key
 * @param value 存储值
 * @param local localStorage或sessionStorage
 */
const $saveStorage = (key, value, local) => {
  let save_key = `${ process.env.VUE_APP_SYSTEM_KEY }_${ key }`;
  let obj = { type: typeof(value), value };
  if (local) {
    localStorage.setItem(save_key, JSON.stringify(obj));
  } else {
    sessionStorage.setItem(save_key, JSON.stringify(obj));
  }
};

/**
 * 获取本地存储
 *
 * @param key 存储key
 */
const $getStorage = key => {
  let save_key = `${ process.env.VUE_APP_SYSTEM_KEY }_${ key }`;
  let val = sessionStorage.getItem(save_key) || localStorage.getItem(save_key);
  if (!val) return;
  try {
    val = JSON.parse(val);
  } catch{
    return val;
  }
  let content = null;
  if (val.type === 'string') {
    content = val.value;
  } else if (val.type === 'number') {
    content = Number(val.value);
  } else if (val.type === 'boolean') {
    content = eval(val.value);
  } else if (val.type === 'object') {
    content = val.value;
  }
  return content;
};

/**
 * 删除本地存储
 *
 * @param key 存储key
 * @param local localStorage或sessionStorage
 */
const $removeStorage = (key, local) => {
  let save_key = `${ process.env.VUE_APP_SYSTEM_KEY }_${ key }`;
  localStorage.removeItem(save_key);
  sessionStorage.removeItem(save_key);
}

/**
 * 清空本地存储
 *
 * @param local localStorage或sessionStorage
 */
const $clearStorage = local => {
  if (local) {
    localStorage.clear();
  } else {
    sessionStorage.clear();
  }
}

export default {
  randomRange: $randomRange,
  randomNum: $randomNum,

  formatDate: $formatDate,
  formatNum: $formatNum,

  listToMap: $listToMap,

  download: $download,

  isFullScreen: $isFullScreen,
  doFullScreen: $doFullScreen,
  exitFullScreen: $exitFullScreen,

  saveStorage: $saveStorage,
  getStorage: $getStorage,
  removeStorage: $removeStorage,
  clearStorage: $clearStorage,
};
